import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getPaperGetUnitTree } from '/@/api/flowManage/flowManage';
import { getDirTreeOpened } from '/@/api/process/process';
export const getDepartmentData = async () => {
  return await getPaperGetUnitTree();
};

export const columns: BasicColumn[] = [
  {
    title: '代理人',
    dataIndex: 'proxyUserRealName',
    width: 100,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '发起人所在部门',
    dataIndex: 'deptNames',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '流程类型',
    dataIndex: 'flowName',
    width: 120,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '开始时间',
    dataIndex: 'beginDate',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '结束时间',
    dataIndex: 'endDate',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
  {
    title: '提醒相关人员',
    dataIndex: 'personRealNameRelated',
    width: 150,
    ellipsis: true,
    resizable: true,
  },
];

export const searchFormSchema: FormSchema[] = [];

function setChildren(node) {
  node.forEach((item) => {
    if (item.code != 'root') {
      item.disabled = item.layer == 3 ? false : true;
    }
    if (item.children && Array.isArray(item.children) && item.children.length > 0) {
      setChildren(item.children);
    }
  });
  return node;
}

export const formSchema: FormSchema[] = [
  {
    field: 'proxyUserRealName',
    label: '代理人',
    required: true,
    component: 'SelectUserInput',
    // helpMessage: '注意：将代理人设置为空将清除代理',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      formModel['hadProxyUserRealNameUsers'] = [];
      if (formModel['proxyUserRealName']) {
        const realNames = formModel['proxyUserRealName']?.split(',');
        const userNames = formModel['proxy']?.split(',');
        realNames?.forEach((item, index) => {
          formModel['hadProxyUserRealNameUsers'].push({
            key: userNames[index],
            name: userNames[index],
            title: item,
            realName: item,
          });
        });
      }
      return {
        type: 0,
        value: formModel.proxyUserRealName,
        hadUsers: formModel['hadProxyUserRealNameUsers'],
        isClear: true,
        onChange: (value, node) => {
          console.log('value', value, node);
          if (value) {
            const { dataRef } = node;
            formModel.proxy = dataRef.userNames;
          } else {
            formModel.proxy = '';
          }
        },
      };
    },
  },
  {
    field: 'proxy',
    label: '代理人值',
    component: 'Input',
    colProps: { span: 24 },
    show: false,
  },
  {
    field: 'starter_param',
    label: '流程发起人所在部门',
    helpMessage: '如果未设置部门，则表示为默认代理，且优先级高',
    component: 'TreeSelect',
    colProps: { span: 24 },
    slot: 'starter_param',
  },
  {
    field: 'flow_code',
    label: '流程类型',
    required: false,
    component: 'ApiTreeSelect',
    colProps: { span: 24 },
    componentProps: {
      api: getDirTreeOpened,
      fieldNames: { label: 'name', value: 'code' },
      showSearch: true,
      treeNodeFilterProp: 'name',
      treeDefaultExpandAll: true,
      getPopupContainer: () => document.body,
      onOptionsChange: (options) => {
        console.log('options', options);
        if (Array.isArray(options) && options.length == 1) {
          setChildren(options);
          options[0].code = '';
        }
      },
    },
  },
  {
    field: 'dates',
    label: '开始/结束时间',
    required: true,
    component: 'RangePicker',
    colProps: { span: 24 },
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      valueForamt: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['开始时间', '结束时间'],
      showTime: { format: 'HH:mm:ss' },
      getPopupContainer: () => document.body,
    },
  },
  {
    field: 'userRealNames',
    label: '提醒相关人员',
    required: false,
    component: 'SelectUserInput',
    colProps: { span: 24 },
    componentProps: ({ formModel }) => {
      formModel['hadUsers'] = [];
      if (formModel['userRealNames']) {
        const realNames = formModel['userRealNames']?.split(',');
        const userNames = formModel['users']?.split(',');
        realNames?.forEach((item, index) => {
          formModel['hadUsers'].push({
            key: userNames[index],
            name: userNames[index],
            title: item,
            realName: item,
          });
        });
      }
      return {
        value: formModel.userRealNames,
        hadUsers: formModel['hadUsers'],
        isClear: true,
        onChange: (value, node) => {
          console.log('value', value, node);
          if (value) {
            const { dataRef } = node;
            formModel.users = dataRef.userNames;
          } else {
            formModel.users = '';
          }
          console.log('record.userNames', formModel.users);
        },
      };
    },
  },
  {
    field: 'users',
    label: '相关人员值',
    component: 'Input',
    colProps: { span: 24 },
    show: false,
  },
];
